package com.easepal7506a.project.manager;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.easepal7506a.project.Constant.CommmandNum;
import com.ogawa.base.network.bean.BeanUpdata;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class DataManager {
    private static DataManager mInst = null;
    private static Object mLock = new Object();
    private Map<String, String> mapState = new HashMap<>();
    private boolean isOpen;//是否开机
    private String mSn/*="OGA75061812010001"*/;//序列号
//    private String mSn = "OGATEST1811210001";//序列号

    private boolean isInitSucceed;
    private boolean isConnectSocket;//是否连接上了socket

    private boolean hasCheckUpdataOk;
    private boolean isNetCanUse;
    private BeanUpdata mUpdataBean;
    private boolean  is7506L;//是否是7506L

    public boolean isIs7506L() {
        return is7506L;
    }

    public void setIs7506L(boolean is7506L) {
        this.is7506L = is7506L;
    }

    public boolean isNetCanUse() {
        return isNetCanUse;
    }

    public void setNetCanUse(boolean netCanUse) {
        isNetCanUse = netCanUse;
    }

    public BeanUpdata getmUpdataBean() {
        return mUpdataBean;
    }

    public void setmUpdataBean(BeanUpdata mUpdataBean) {
        this.mUpdataBean = mUpdataBean;
    }

    public boolean isInitSucceed() {
        return isInitSucceed;
    }

    public boolean isHasCheckUpdataOk() {
        return hasCheckUpdataOk;
    }

    public void setHasCheckUpdataOk(boolean hasCheckUpdata) {
        this.hasCheckUpdataOk = hasCheckUpdata;
    }

    public void setInitSucceed(boolean initSucceed) {
        isInitSucceed = initSucceed;
    }

    public boolean isOpen() {
        return !TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusRunning)) && !DataManager.getInst().getValue(CommmandNum.mStatusRunning).equals("0");
    }

    public boolean isPause() {
        return !TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusRunning)) && DataManager.getInst().getValue(CommmandNum.mStatusRunning).equals("3");
    }

    public void setOpen(boolean open) {
        isOpen = open;
    }


    public boolean isConnectSocket() {
        return isConnectSocket;
    }

    public void setConnectSocket(boolean connectSocket) {
        isConnectSocket = connectSocket;
    }

    public String getmSn() {
        return mSn;
    }

    public void setmSn(String mSn) {
        this.mSn = mSn;
    }
    private DataManager() {
    }

    public static DataManager getInst() {
        if (mInst == null) {
            synchronized (mLock) {
                mInst = new DataManager();
            }
        }
        return mInst;

    }


    public void clearMap() {
        if (mapState != null) {
            mapState.clear();
        }
    }

    public void putKeyValue(String key, String value) {
        if (mapState != null) {
            mapState.put(key, value);
        }


    }

    public String getAuto() {
        if (mapState != null && mapState.get(CommmandNum.mStatusAuto) != null) {
            return mapState.get(CommmandNum.mStatusAuto);
        } else {
            return "0";
        }
    }

    public String getHand() {
        if (mapState != null && mapState.get(CommmandNum.mStatusHand) != null) {
            return mapState.get(CommmandNum.mStatusHand);
        } else {
            return "0";
        }
    }

    public String getKnead() {
        if (mapState != null && mapState.get(CommmandNum.mStatusKnead) != null) {
            return mapState.get(CommmandNum.mStatusKnead);
        } else {
            return "0";
        }
    }

    public String getLed() {
        if (mapState != null && mapState.get(CommmandNum.mStatusLed) != null) {
            return mapState.get(CommmandNum.mStatusLed);
        } else {
            return "0";
        }
    }

    public String getHot() {
        if (mapState != null && mapState.get(CommmandNum.mStatusHot) != null) {
            return mapState.get(CommmandNum.mStatusHot);
        } else {
            return "0";
        }
    }

    public String getJdgl() {
        if (mapState != null && mapState.get(CommmandNum.mStatusJdgl) != null) {
            return mapState.get(CommmandNum.mStatusJdgl);
        } else {
            return "0";
        }
    }

    public String getAirJb() {
        if (mapState != null && mapState.get(CommmandNum.mStatusAirShoulder) != null) {
            return mapState.get(CommmandNum.mStatusAirShoulder);
        } else {
            return "0";
        }
    }
    public String getAirBack() {
        if (mapState != null && mapState.get(CommmandNum.mStatusAirSit) != null) {
            return mapState.get(CommmandNum.mStatusAirSit);
        } else {
            return "0";
        }
    }
    public String getAirHand() {
        if (mapState != null && mapState.get(CommmandNum.mStatusAirHand) != null) {
            return mapState.get(CommmandNum.mStatusAirHand);
        } else {
            return "0";
        }
    }
    public String getAirLeg() {
        if (mapState != null && mapState.get(CommmandNum.mStatusAirLeg) != null) {
            return mapState.get(CommmandNum.mStatusAirLeg);
        } else {
            return "0";
        }
    }
    public String getAirAll() {
        if (mapState != null && mapState.get(CommmandNum.mStatusAirAll) != null) {
            return mapState.get(CommmandNum.mStatusAirAll);
        } else {
            return "0";
        }
    }

    public boolean isAdvanceRunning() {
        Log.e("isAdvanceRunning","auto=="+DataManager.getInst().getAuto()+"==hand=="+DataManager.getInst().getHand()+"==knead=="+DataManager.getInst().getKnead()+"==hot=="+DataManager.getInst().getHot()+"==led=="+DataManager.getInst().getLed()+"==jdgl=="+DataManager.getInst().getJdgl());

        return !getHand().equals("0") || !getKnead().equals("0") || !getHot().equals("0") || !getJdgl().equals("0") || !getLed().equals("0")|| !getAirJb().equals("0")
                || !getAirBack().equals("0")  || !getAirHand().equals("0")  || !getAirLeg().equals("0")  || !getAirAll().equals("0");
    }

    public boolean isAutoRunning() {
        return !getAuto().equals("0");
    }

    public boolean isRunning(){
        return isAdvanceRunning()||isAutoRunning();
    }

    public String getValue(String key) {
        String value = "";
        if (mapState != null) {
            value = mapState.get(key);
        }
        return (value == null) ? "" : value;
    }

    /**
     * 日志。。。。
     */

    /**
     * 创建文件或文件夹
     *
     * @param fileName 文件名或问文件夹名
     */
    public static File createFile(String fileName) {
        File file = new File(getSDCardPath() + fileName);
        System.out.println("创建了文件夹" + getSDCardPath() + fileName);

        if (fileName.indexOf(".") != -1) {
            // 说明包含，即使创建文件, 返回值为-1就说明不包含.,即使文件
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("创建了文件");
        } else {
            // 创建文件夹
            file.mkdir();
            System.out.println("创建了文件夹");
        }
        return file;
    }

    private static String SDPATH = "";

    public static File creatFile() {
        return createFile("Log7506");

    }

//   public static void setLog(String commond){
//       Logger.file(LogTag.mk("sendCommond"), "sendCommond.txt", dir, new NullPointerException(), commond);
//   }

    /**
     * 获取到sd卡的根目录，并以String形式返回
     *
     * @return
     */
    public static String getSDCardPath() {
        SDPATH = Environment.getExternalStorageDirectory() + "/";
        return SDPATH;
    }
}
